//
//  BZCircleRatioView.m
//  BezierDemo
//
//  Created by yuanjiang on 2017/12/14.
//  Copyright © 2017年 yuanjiang. All rights reserved.
//

#import "BZCircleRatioView.h"
#import "BZCircleModel.h"

/** 白色圆的半径*/
static CGFloat const whiteCircleRadius = 25.0;
/** 指引线的小圆*/
static CGFloat const smallCircleRadius = 4.0;
/** 指引线文字字体的大小*/
static  CGFloat const nameTextFont = 10.0;
/** 指引线的宽度*/
static CGFloat const lineWidth = 60.0;
/** 折线的宽度*/
static CGFloat const foldLineWidth = 10.0;

#define degreeToRadius(x) (180.0 / PI * (x))

@implementation BZCircleRatioView

-(instancetype)initWithFrame:(CGRect)frame
                   dataAaary:(NSMutableArray *)array
                circleRadius:(CGFloat)circleRaduius{
    if (self = [super initWithFrame:frame]) {
        self.dataArray = array;
        self.circleRadius = circleRaduius;
    }
    return self;
}

- (CGFloat)getShareNumber:(NSMutableArray *)arr{
    CGFloat f = 0.0;
    for (int i = 0; i< arr.count; i++) {
        BZCircleModel *model = arr[i];
        f += [model.number floatValue];
    }
    return M_PI * 2 / f;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    
    [@"复垦节课" drawInRect:CGRectMake(0, 0, 100, 40) withAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
    // Drawing code
    //1. 所占比例
    CGFloat b1 = [self getShareNumber:self.dataArray];
    //2.开启上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGFloat angle_start = 0; //开始时的弧度 --- 旋转200度
    CGFloat ff = 0;//记录偏转的角度 --- 旋转200度
    
    for (int i = 0; i < self.dataArray.count; i++) {
        BZCircleModel *model = self.dataArray[i];
        CGFloat angle_end = [model.number floatValue] * b1 + ff; //结束
        ff += [model.number floatValue] *b1;
        NSLog(@"angle_end == %f",angle_end);
        
        [self drawArcWithCGContextRef:ctx point:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2) Angle_start:angle_start Angle_end:angle_end Color:model.color Int:i];
        angle_start =  angle_end;
        /**
         参数
         上下文
         中心点
         开始
         结束
         颜色
         
         */
    }
    //3.添加中心圆
    [self addCenterCircle];
}

/**
 添加中心圆
 */
- (void)addCenterCircle{
    UIBezierPath *centerCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:whiteCircleRadius startAngle:0 endAngle:M_PI *2 clockwise:YES];
    UIColor *color = [UIColor whiteColor];
    [color set];
    [centerCircle fill];
    [centerCircle stroke];
    
}

/**
 画圆弧

 @param ctx 上下文
 @param point 圆心
 @param angle_start 开始角度
 @param angle_end 结束角度
 @param color 颜色
 @param n 表示第几个弧形
 */
- (void)drawArcWithCGContextRef:(CGContextRef)ctx
                          point:(CGPoint)point
                    Angle_start:(float)angle_start
                      Angle_end:(float)angle_end
                          Color:(UIColor *)color
                            Int:(int)n{
    
    //1.开始画线
    CGContextMoveToPoint(ctx, point.x, point.y);
    //2.颜色空间填充
    CGContextSetFillColor(ctx, CGColorGetComponents(color.CGColor));
//    //3.画圆   CGContextAddArc 核心方法
    CGContextAddArc(ctx, point.x, point.y, self.circleRadius, angle_start, angle_end, 0);
//    //4.填充
    CGContextFillPath(ctx);
    //5.弧度的中心角度
    CGFloat h = (angle_end + angle_start) / 2.0;
    //6.小圆的中心点
    CGFloat xx = self.frame.size.width /2 + (_circleRadius + 10) * cos(h);
    CGFloat yy = self.frame.size.height / 2 + (_circleRadius + 10) * sin(h);
    //7.画折线
    [self addLineAndnumber:color CGContextRef:ctx X:xx Y:yy Int:n angle:h];
    
}

/**
 折线

 @param color 颜色
 @param ctx CGContextRef
 @param x 小圆的中心点x
 @param y 小圆的中心点y
 @param n 表示第几个弧形
 @param angle 弧度的中心角度
 */
-(void)addLineAndnumber:(UIColor*)color
           CGContextRef:(CGContextRef)ctx
                      X:(CGFloat)x
                      Y:(CGFloat)y
                    Int:(int)n
                  angle:(CGFloat)angle
{
    //1. 小圆的圆心
    CGFloat smallCircleCenterPointX = x;
    CGFloat smallCircleCenterPointY = y;
    //2.折点
    CGFloat lineLosePointx = 0.0;//指引线的折点
    CGFloat lineLosePoiney = 0.0;
   
    //3.指引线的终点
    CGFloat lineEndPointx ;
    CGFloat lineEndPointy = 0.0 ;
    
    //4.数字的起点
    CGFloat numberStartX;
    CGFloat numberStartY;
    
    //5.文字的起点
    CGFloat textStarX;
    CGFloat textStarY;
    
    //6.数字的长度
    BZCircleModel *model = self.dataArray[n];
    NSString *number = model.number;
    CGSize numbersize = [number sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0]}];
    //文字size
    CGSize textSize = [model.name sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:nameTextFont]}];
    
    //设置折点
    lineLosePointx = smallCircleCenterPointX + foldLineWidth * cos(angle);
    lineLosePoiney = smallCircleCenterPointY + foldLineWidth * sin(angle);
    
    //7。画小圆
    if (smallCircleCenterPointX > self.bounds.size.width * 0.5) {
        //指引线终点
        lineEndPointx = lineLosePointx + lineWidth;
        lineEndPointy = lineLosePoiney;
        //数字
        numberStartX = lineEndPointx - numbersize.width;
        numberStartY = lineEndPointy - numbersize.height;
        //文字
        textStarX = lineEndPointx - textSize.width;
        textStarY = lineEndPointy;
        
    }else{
        //指引线终点
        lineEndPointx = lineLosePointx - lineWidth;
        lineEndPointy = lineLosePoiney;
        //数字
        numberStartX = lineEndPointx;
        numberStartY = lineEndPointy - numbersize.height;
        //文字
        textStarX = lineEndPointx;
        textStarY = lineEndPointy;
    }
    //8. 画小圆
    /*!创建圆弧
     参数：
     center->圆点
     radius->半径
     startAngle->起始位置
     endAngle->结束为止
     clockwise->是否顺时针方向
     */
    UIBezierPath *arcPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(smallCircleCenterPointX, smallCircleCenterPointY) radius:smallCircleRadius startAngle:0 endAngle:M_PI * 2 clockwise:YES];
    [color set];
    //填充
    [arcPath fill];
    //描边，路径创建需要描边才能显示出来
    [arcPath stroke];
    
    //9.画指引线
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, smallCircleCenterPointX, smallCircleCenterPointY);
    CGContextAddLineToPoint(ctx, lineLosePointx, lineLosePoiney);
    CGContextAddLineToPoint(ctx, lineEndPointx, lineEndPointy);
    CGContextSetLineWidth(ctx, 1);
    CGContextSetFillColorWithColor(ctx, color.CGColor);
    CGContextStrokePath(ctx);
    
    NSLog(@"小圆 %f,%f",smallCircleCenterPointX,smallCircleCenterPointY);
    NSLog(@"折点 %f,%f",lineLosePointx,lineLosePoiney);
    NSLog(@"终点 %f,%f",lineEndPointx,lineEndPointy);
    
    //10.画指引线上的数字
    [model.number drawAtPoint:CGPointMake(numberStartX, numberStartY) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10.0], NSForegroundColorAttributeName:color}];
    //11.画指引线下的文字
    // 11.1设置段落风格
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.alignment = NSTextAlignmentRight;
    if (lineEndPointx < [UIScreen mainScreen].bounds.size.width / 2.0) {
        paragraph.alignment = NSTextAlignmentLeft;
    }
    [model.name drawInRect:CGRectMake(textStarX, textStarY, textSize.width, textSize.height) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:nameTextFont], NSForegroundColorAttributeName:color,NSParagraphStyleAttributeName:paragraph}];
}

//重绘








































@end
